package cn.wangzg.course0501;

import androidx.appcompat.app.AppCompatActivity;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.Toast;

public class LoginActivity extends AppCompatActivity {
    private EditText et_name;
    private EditText et_pwd;
    private CheckBox cb_remPwd;
    private CheckBox cb_showPwd;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        initView();
    }

    private void initView() {
        et_name = findViewById(R.id.editText);
        et_pwd = findViewById(R.id.editText2);//密码输入
        cb_remPwd = findViewById(R.id.checkBox);
        cb_showPwd = findViewById(R.id.checkBox2);//显示密码

        cb_showPwd.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    et_pwd.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                } else {
                    et_pwd.setTransformationMethod(PasswordTransformationMethod.getInstance());
                }
            }
        });

        SharedPreferences sp=getSharedPreferences("aaa",MODE_PRIVATE);
        String name=sp.getString("name","");
        String pwd=sp.getString("pwd","");
        if(!"".equals(name)){
            et_name.setText(name);
        }
        if(!"".equals(pwd)){
            et_pwd.setText(pwd);
        }
    }

    public void btnLogin(View view){
        //1.获取用户名和密码
        String name=et_name.getText().toString();
        String pwd=et_pwd.getText().toString();
        //2.判断是否正确
        if("admin".equals(name) && "123456".equals(pwd)){
            if(cb_remPwd.isChecked()){
                SharedPreferences sp=getSharedPreferences("aaa",MODE_PRIVATE);
                SharedPreferences.Editor edit = sp.edit();
                edit.putString("name",name);
                edit.putString("pwd",pwd);
                edit.commit();
            }
            //startActivity(new Intent(this,));
            Toast.makeText(this, "success!", Toast.LENGTH_SHORT).show();
        }
        //3.正确，登录，记住密码
    }
}
